package com.ecnu.sei.app.pcm;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import com.ecnu.sei.app.resources.Classroom;
import com.ecnu.sei.app.teacher.AbstractTeacher;
import com.ecnu.sei.app.teacher.Administrator;
import com.ecnu.sei.app.teacher.AssociateProfessor;
import com.ecnu.sei.app.teacher.ForeignTeacher;
import com.ecnu.sei.app.teacher.Lecturer;
import com.ecnu.sei.app.teacher.Official;
import com.ecnu.sei.app.teacher.Professor;
import com.ecnu.sei.app.util.Pair;
import com.ecnu.sei.app.util.Utils;
import com.ecnu.sei.app.util.Utils.CollegeYear;
import com.ecnu.sei.app.util.Utils.DayHour;

public class GenerateData {

  // private int teacherIdLength = 3;
  // private int courseIdLength = 10;
  private int courseCount = 20;
//  private String className = null;

  private String[] lastNames = { "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯",
      "陈", "褚", "卫", "蒋", "沈", "韩", "杨", };

  private String[] letters = { "A", "B", "C", "D", "E", "F", "G", "H", "I",
      "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
      "X", "Y", "Z" };

  private String[] grades = { "12", "11", "10", "09" };
  private String[] classes = { "01", "02", "03", "04" };
  private String[] classrooms = { "B204", "B208", "B210" };
  private int[] courseTimes = { 8, 10, 13, 15, 18 };
  private int[] weekdays = { 1, 2, 3, 4, 5 };

  private Random random = new Random(System.currentTimeMillis());

  /*
   * public int getTeacherIdLength() { return this.teacherIdLength; }
   * 
   * public void setTeacherIdLength(int length) { this.teacherIdLength = length;
   * }
   * 
   * public int getCourseIdLength() { return this.courseIdLength; }
   * 
   * public void setCourseIdLength(int length) { this.courseIdLength = length; }
   */
  public int getCourseCount() {
    return this.courseCount;
  }

  public void setCourseCount(int count) {
    this.courseCount = count;
  }

  /*
   * private String generateId(int length) { String courseIdString = new
   * String();
   * 
   * for (int i = 0; i < length; i ++) { int numIndex = 1; if (0 == i) {
   * numIndex = Math.abs(random.nextInt() % 9) + 1; } else { numIndex =
   * Math.abs(random.nextInt() % 10); }
   * 
   * String numStr = String.valueOf(numIndex); courseIdString += numStr; }
   * 
   * return courseIdString;
   * 
   * }
   */
  private String generateId() {
    return UUID.randomUUID().toString();
  }

  public String generateCourseId() {
    //return this.generateId(this.courseIdLength);
    return this.generateId();
  }

  public String generateTeacherId() {
    //return this.generateId(this.teacherIdLength);
    return this.generateId();
  }

  public String generateClassName() {
    String classNameStr = new String();

    int gradeIndex = Math.abs(random.nextInt() % 4);
    int classIndex = 0;
    if (3 == gradeIndex) {
      classIndex = Math.abs(random.nextInt() % 3);
    } else {
      classIndex = Math.abs(random.nextInt() % 4);
    }

    String gradeStr = grades[gradeIndex];
    String classStr = classes[classIndex];

    classNameStr += gradeStr;
    classNameStr += classStr;

//    this.className = classNameStr;

    return classNameStr;
  }

  public CollegeYear deriveCollegeYear(String className) {
    String gradeStr = className.substring(0, 2);

    for (int i = 0; i < grades.length; i++) {
      String grade = grades[i];
      if (gradeStr.equals(grade)) {
        return CollegeYear.getList()[i];
      }
    }

    return null;
  }

  public String generateCourseName() {
    String classNameStr = new String("课程");

    int numIndex = Math.abs(random.nextInt() % this.courseCount);
    classNameStr += String.valueOf(numIndex);

    return classNameStr;
  }

  private int[] generateLotteryArray(int[] lottery, int startIndex, int count,
      int num) {
    for (int i = startIndex; i < startIndex + count; i++) {
      lottery[i] = num;
    }

    return lottery;
  }

  private int[] removeItemFromLotteryArray(int[] lottery, int index) {
    int newArrayLength = lottery.length - 1;
    int[] newLotteryArray = new int[newArrayLength];

    for (int a = 0; a < index; a++) {
      newLotteryArray[a] = lottery[a];
    }

    for (int b = index + 1; b < lottery.length; b++) {
      newLotteryArray[b - 1] = lottery[b];
    }

    return newLotteryArray;
  }

  private String generateTeacherName() {
    String teacherNameStr = new String();

    int length = Math.abs(random.nextInt() % 2) + 2;

    for (int i = 0; i < length; i++) {
      if (0 == i) {
        int lastNameIndex = Math.abs(random.nextInt() % lastNames.length);
        String lastNameStr = lastNames[lastNameIndex];
        teacherNameStr += lastNameStr;
      } else {
        int letterIndex = Math.abs(random.nextInt() % letters.length);
        String letterStr = letters[letterIndex];
        teacherNameStr += letterStr;
      }
    }

    return teacherNameStr;
  }

  public List<AbstractTeacher> generateTeacher(int n1, int n2, int n3, int n4,
      int n5, int n6) {
    int totalCount = n1 + n2 + n3 + n4 + n5 + n6;

    int[] teacherCount = { n1, n2, n3, n4, n5, n6 };
    int[] lottery = new int[totalCount];
    List<AbstractTeacher> teacherList = new ArrayList<AbstractTeacher>();

    for (int i = 0; i < teacherCount.length; i++) {
      int currentNum = i + 1;
      int currentCount = teacherCount[i];
      int startIndex = 0;

      for (int n = 0; n < i; n++) {
        startIndex += teacherCount[n];
      }

      lottery = this.generateLotteryArray(lottery, startIndex, currentCount,
          currentNum);
    }

    int lotteryBaseCount = lottery.length;
    int[] newLottery = lottery;
    AbstractTeacher teacher = null;
    for (int t = 0; t < totalCount; t++) {
      int numIndex = Math.abs(random.nextInt() % lotteryBaseCount);
      int numValue = newLottery[numIndex];

      newLottery = this.removeItemFromLotteryArray(newLottery, numIndex);
      lotteryBaseCount = newLottery.length;

      String teacherId = this.generateId();
      String teacherName = this.generateTeacherName();

      switch (numValue) {
      case 1:
        teacher = new Administrator(teacherId, teacherName);
        break;
      case 2:
        teacher = new AssociateProfessor(teacherId, teacherName);
        break;
      case 3:
        teacher = new ForeignTeacher(teacherId, teacherName);
        break;
      case 4:
        teacher = new Lecturer(teacherId, teacherName);
        break;
      case 5:
        teacher = new Official(teacherId, teacherName);
        break;
      case 6:
        teacher = new Professor(teacherId, teacherName);
        break;
      default:
        break;
      }

      teacherList.add(teacher);
    }

    return teacherList;
  }

/*  public List<Pair<Integer, DayHour>> generateCourseTimeList() {
    List<Pair<Integer, DayHour>> courseTimeList = new ArrayList<Pair<Integer, DayHour>>();

    int numberOfCoursesPerWeek = Math.abs(random.nextInt() % 3) + 1;
    for (int i = 0; i < numberOfCoursesPerWeek; i++) {
      int weekdayIndex = Math.abs(random.nextInt() % weekdays.length);
      int weekOfDay = weekdays[weekdayIndex];
      int dayHourIndex = Math.abs(random.nextInt() % courseTimes.length);
      int dayHourTime = courseTimes[dayHourIndex];

      DayHour dayHour = Utils.getDayHourFromStartTime(dayHourTime);
      Pair<Integer, DayHour> courseTime = new Pair<Integer, DayHour>(weekOfDay,
          dayHour);

      courseTimeList.add(courseTime);
    }

    return courseTimeList;
  }*/
  
  public List<Pair<Integer, DayHour>> generateCourseTimeList() {
    List<Pair<Integer, DayHour>> timeList = new ArrayList<Pair<Integer, DayHour>>();
    for (int i = 0; i < weekdays.length; i++) {
      for (int j = 0; j < courseTimes.length; j++) {
        timeList.add(new Pair<Integer, DayHour>(weekdays[i], Utils.getDayHourFromStartTime(courseTimes[j])));
      }
    }
    return timeList;
  }

  public int generateStudentNumber(int rangeStart, int rangeEnd) {
    int rangeLength = rangeEnd - rangeStart;
    int studentNum = rangeStart + Math.abs(random.nextInt() % rangeLength);

    return studentNum;
  }

  public List<Classroom> generateClassroomList() {
    List<Classroom> classroomList = new ArrayList<Classroom>();

    int classroomIndex = Math.abs(random.nextInt() % classrooms.length);
    String classroomName = classrooms[classroomIndex];
    Classroom classroom = new Classroom(classroomName);
    classroomList.add(classroom);

    return classroomList;
  }
}
